package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.cjt.domain.TFstock;
import com.mjf.cjt.mapper.TFstockMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.TFstockService;
import com.mjf.cjt.vo.InventoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/5/18 04:07
 */
@Service
public class TFstockServiceImpl extends ServiceImpl<TFstockMapper, TFstock> implements TFstockService {

    @Autowired
    private AsyncTMService asyncTMService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncCH() {
        List<InventoryVO.InventoryInfo> inventoryInfos = asyncTMService.asyncCH();
        if (CollectionUtil.isEmpty(inventoryInfos)) {
            return;
        }
        List<TFstock> tfstocks = inventoryInfos.stream().map(inventoryInfo -> {
            TFstock tfstock = new TFstock();
            tfstock.setStid(String.valueOf(inventoryInfo.getId()));
            tfstock.setStcode(inventoryInfo.getCode());
            tfstock.setStname(inventoryInfo.getName());
            tfstock.setStspec(inventoryInfo.getSpecification());
            tfstock.setStunit(inventoryInfo.getBaseunitname());
            tfstock.setStunitcode(inventoryInfo.getBaseunitcode());
            tfstock.setStclasscode(inventoryInfo.getInventoryClass().getCode());
            tfstock.setStclassname(inventoryInfo.getInventoryClass().getName());
            return tfstock;
        }).collect(Collectors.toList());
        saveOrUpdateBatch(tfstocks, 500);
    }

}
